<%@ taglib prefix="page" uri="http://com.qipay/pagelib" %>
<%@ page language="java" import="com.qipay.admin.order.vo.OrderVo" contentType="text/html;charset=utf-8" %>
<%@ page import="com.qipay.admin.tags.PageModel" %>
<%@ page import="com.qipay.admin.utils.SiteHelper" %>
<%@ page import="com.qipay.user.entity.UserLevel" %>
<%@ page import="org.apache.commons.collections.CollectionUtils" %>
<%@ page import="java.util.List" %>
<%@ page import="java.util.Map" %>
<%@ page import="com.qipay.order.config.OrderStateEnum" %>
<%@ page import="org.apache.commons.lang3.StringUtils" %>
<%@ page import="com.qipay.baas.model.Sp" %>
<!doctype html>
<html>
<head>
    <meta name="viewport"
          content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no" />
    <jsp:include page="head.jsp"></jsp:include>
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/dataTables.bootstrap.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/bootstrap-datepicker3.min.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/datetimepicker.css">
    <link rel="stylesheet" href="<%= request.getAttribute("StaticHost") %>/css/list.css">

    <script type="text/javascript">
        //document.getElementsByTagName("html")[0].style.width=screen.availWidth/100+"px";
        document.getElementsByTagName("html")[0].style.fontSize=screen.availWidth/200+"px";
    </script>
    <style type="text/css">
        .border{
            border:1px solid red;
        }
    </style>
</head>
</head>
<body class="hold-transition skin-blue sidebar-mini">
<div class="wrapper">
    <jsp:include page="nav.jsp"></jsp:include>
    <jsp:include page="left.jsp"></jsp:include>
    <!-- Content Wrapper. Contains page content -->
    <div class="content-wrapper">
        <!-- Main content -->
        <section class="content ">
            <div class="row" >
                <div class="col-md-12">
                    <div class="box">
                        <%--Title--%>
                        <div class="box-header">
                            <h3 class="box-title" style="font-size: 2rem;">订单列表</h3>
                        </div>
                        <%--Search--%>
                        <div >
                            <form id="searchForm" action="/order" >
                                <%
                                    Map<String, String> params = (Map<String, String>) request.getAttribute("params");
                                    UserLevel userLevel = (UserLevel) request.getAttribute("level");
                                    Long amountCount = (Long) request.getAttribute("amountCount");
                                    Long feeCount = (Long) request.getAttribute("feeCount");

                                %>
                                <% if (userLevel == UserLevel.ADMIN || userLevel == UserLevel.AGENT) {%>
                                <div class="new-group">
                                    <label for="cpId">商户 ID：</label>
                                    <input type="text" id="cpId" name="cpId" placeholder="商户 ID"
                                           value="<%= params.containsKey("cpId")?params.get("cpId"):""%>"/>
                                </div>
                                <%}%>

                                <%--
                                <div class="new-group">
                                    <label for="spTradeNo">SP 订单：</label>
                                    <input type="text" id="spTradeNo" name="spTradeNo"
                                           placeholder="SP 订单"
                                           value="<%= params.containsKey("spTradeNo") ?params.get("spTradeNo"):""%>"/>
                                </div>
                                --%>
                                <div class="new-group">
                                    <label for="tradeNo">系统订单：</label>
                                    <input type="text" id="tradeNo" name="tradeNo"
                                           placeholder="系统订单"
                                           value="<%= params.containsKey("tradeNo") ?params.get("tradeNo"):""%>"/>
                                </div>
                                <div class="new-group">
                                    <label for="outTradeNo">商户订单：</label>
                                    <input type="text" id="outTradeNo" name="outTradeNo"
                                           placeholder="商户订单"
                                           value="<%= params.containsKey("outTradeNo") ?params.get("outTradeNo"):""%>"/>
                                </div>
                                <% if (userLevel == UserLevel.ADMIN) {%>
                                <div class="new-group">
                                    <label for="outTradeNo">SPID：</label>
                                    <input type="text" id="spId" name="spId"
                                           placeholder="SPID"
                                           value="<%= params.containsKey("spId") ?params.get("spId"):""%>"/>
                                </div>
                                <%}%>
                                <div class="new-group" >
                                    <label for="channel">支付方式：</label>
                                    <select id="channel" name="channel">
                                        <%
                                            Map<Integer, String> channelMap = (Map<Integer, String>) request.getAttribute("channels");
                                            Integer channel = params.containsKey("channel") ? Integer.valueOf(params.get("channel")) : 0;
                                            for (Integer key : channelMap.keySet()) {
                                                String value = channelMap.get(key);
                                        %>
                                        <option value="<%=key%>"
                                                <% if (null != channel && channel.equals(key)) {%>
                                                selected="selected"<%}%>>
                                            <%=value%>
                                        </option>
                                        <%}%>
                                    </select>
                                </div>
                                <div class="new-group">
                                    <label for="state">订单状态：</label>
                                    <select id="state" name="state">
                                        <%
                                            Map<Short, String> stateMap = (Map<Short, String>) request.getAttribute("states");
                                            Short state = params.containsKey("state") ?Short.valueOf(params.get("state")) : 0;
                                            for (Short key : stateMap.keySet()) {
                                                String value = stateMap.get(key);
                                        %>
                                        <option value="<%=key%>"
                                                <% if (null != state && state.equals(key)) {%>
                                                selected="selected"<%}%>>
                                            <%=value%>
                                        </option>
                                        <%}%>
                                    </select>
                                </div>
                                <div class="new-group date form_datetime">
                                    <label for="from">开始日期：</label>
                                    <input type="text" id="from" name="from" readonly
                                           value="<%= params.containsKey("from") ?params.get("from"):""%>" data-date-format="yyyy-mm-dd hh:ii">
                                </div>
                                <div class="new-group date form_datetime">
                                    <label for="to">结束日期：</label>
                                    <input type="text" id="to" name="to" readonly
                                           value="<%= params.containsKey("to") ?params.get("to"):""%>" data-date-format="yyyy-mm-dd hh:ii">
                                </div>
                                <div style="clear: both"></div>
                                <div class="button-group">
                                    <button type=" submit" class="button back-gray" id="btnSearch" style="font-size: 1rem">查询</button>
                                    <a href="" id="btnDownload" download="订单明细.xls" target="_blank">
                                        <button type="button" class="button back-gray" style="font-size: 1rem">导出</button>
                                    </a>
                                </div>
                            </form>
                            <div style="clear: both"></div>
                        </div>
                        <!-- /.box-header -->
                        <div class="tableContent">
                            <%--class="table table-bordered table-hover"--%>
                            <table id="orderLst"  width="100%" border="1" cellpadding="0" cellspacing="0">
                                <thead>
                                <tr>
                                    <th width="5%">ID</th>
                                    <th width="10%">商户名称</th>
                                    <th width="5%">商户 ID</th>
                                    <% if (userLevel == UserLevel.ADMIN) {%>
                                    <th width="3%">SPID</th>
                                    <%}%>
                                    <th width="5%">商品描述</th>
                                    <th width="14%">订单</th>
                                    <th width="5%">金额</th>
                                    <th width="4%">手续费</th>
                                    <% if (userLevel == UserLevel.ADMIN) {%>
                                    <th width="6%">SP渠道</th>
                                    <%}%>
                                    <th width="5%">支付方式</th>
                                    <th width="5%">状态</th>
                                    <th width="5%">操作</th>
                                </tr>
                                </thead>
                                <tbody>
                                <%
                                    List<List<String>> list = (List<List<String>>) request.getAttribute("lst");
                                    PageModel pager = (PageModel) request.getAttribute("page");
                                    String url = pageContext.getRequest().getServletContext().getContextPath() + "/order" + (StringUtils.isNotBlank(pager.getUrl()) ? pager.getUrl() : "");
                                    if (CollectionUtils.isNotEmpty(list)) {
                                        for (List<String> p : list) {
                                %>
                                <tr id="order_<%=p.get(0)%>">
                                    <td>
                                        <a href="/order/<%= p.get(0)%>" style="color: #778899;">
                                            <%= p.get(0)%>
                                        </a>
                                    </td>
                                    <td>
                                        <% if (userLevel == UserLevel.ADMIN) {%>
                                        <a href="/cp/<%= p.get(2)%>" style="color: #778899;">
                                            <%= p.get(1)%>
                                        </a>
                                        <%
                                        } else {
                                        %>
                                        <%= p.get(1)%>
                                        <%}%>
                                    </td>
                                    <td>
                                        <% if (userLevel == UserLevel.ADMIN) {%>
                                        <a href="/cp/<%= p.get(2)%>" style="color: #778899;">
                                            <%= p.get(2)%>
                                        </a>
                                        <%
                                        } else {
                                        %>
                                        <%= p.get(2)%>
                                        <%}%>
                                    </td>

                                    <% if (userLevel == UserLevel.ADMIN) {%>
                                    <td>
                                        <%= p.get(3)%>
                                    </td>
                                    <%}%>
                                    <td>
                                        <%= p.get(4)%>
                                    </td>
                                    <td>
                                        系统订单<%= p.get(5)%><br>
                                        商户订单<%= p.get(6)%><br>
                                        <%--SP 订单<%=p.get(7)%><br>--%>
                                        创建时间<%= p.get(8)%><br>
                                        完成时间<%= p.get(9)%>
                                    </td>
                                    <td>
                                        <%= p.get(10)%>
                                    </td>
                                    <td>
                                        <%= p.get(11)%>
                                    </td>
                                    <% if (userLevel == UserLevel.ADMIN) {%>
                                        <td><%=p.get(15)%></td>
                                    <%}%>
                                    <td>
                                        <%= p.get(12)%>
                                    </td>
                                    <td>
                                        <%=p.get(13)%>
                                    </td>

                                    <td>
                                        <% if (userLevel == UserLevel.ADMIN && OrderStateEnum.SUCCESS.getType().equals(Short.parseShort(p.get(14)))) {%>
                                        <a name="lnkFreeze" pk="<%= p.get(0)%>" href="javascript:;">
                                            <button type="button" class="button back-gray" style="font-size: 1rem">冻结</button>
                                        </a>
                                        <a name="lnkNotify" id="<%= p.get(5)%>" pk="<%= p.get(5)%>" href="javascript:;">
                                            <button type="button" class="button back-blue" style="font-size: 1rem;color: #778899;background: #eeeeee">通知</button>
                                        </a>

                                        <%}else if(userLevel == UserLevel.ADMIN){%>
                                        <a name="lnkSetSuccess" pk="<%= p.get(0)%>" href="javascript:;">
                                            <button type="button" class="button back-gray"style="font-size: 1rem">置成功</button></a>
                                        <%}%>
                                        <% if (userLevel == UserLevel.ADMIN && OrderStateEnum.FREEZED.getType().equals(Short.parseShort(p.get(14)))) {%>
                                        <a name="lnkUnfreeze" pk="<%= p.get(0)%>" href="javascript:;">
                                            <button type="button" class="button back-gray"style="font-size: 1rem" >解冻</button>
                                        </a>
                                        <%}%>
                                    </td>
                                </tr>

                                <%
                                    }
                                } else {
                                %>
                                <tr>
                                    <td colspan="9">未找到数据</td>
                                </tr>
                                <% }%>
                                </tbody>

                            </table>
                            <tr>
                                <td colspan="18">总金额 :<%=SiteHelper.formatMoney(amountCount, 100.0)%>&nbsp&nbsp</td>
                                <td colspan="18">手续费 :<%=SiteHelper.formatMoney(feeCount, 100.0)%>&nbsp&nbsp</td>
                                <td colspan="18">总笔数：<%= pager.getTotal()%>&nbsp&nbsp</td>
                            </tr>
                            <div class="col-sm-10">
                                <%if (CollectionUtils.isNotEmpty(list)) {%>
                                <div id="orderLst_paginate">
                                    <page:page totalSum="<%= pager.getTotal()%>"
                                               url="<%= url%>"
                                               pageNo="<%= pager.getPage()%>"
                                               pageSize="<%= 30 %>" />

                                </div>
                                <div style="position: relative;bottom:1rem;" >
                                    <input onkeydown="javascript:if(event.keyCode==13) gotoPage();"  value="<%= pager.getPage()%>" type="number" id="pz"
                                           style="text-align:center;height:2.5rem;width: 5%;border-radius: 0.2rem;border: 1px solid #dcdcdc;"/>
                                    <span onclick="gotoPage();" style="height:2.5rem;line-height:2.5rem;background: #f5f5f5;cursor: pointer;
                                    display: inline-block;width: 3rem;border: 1px solid #dcdcdc;text-align: center;border-radius: 2px;position: relative;top:1px;">go</span>
                                </div>
                                <% }%>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </section>
    </div>
    <jsp:include page="foot.jsp"></jsp:include>
</div>
<%--<script src="<%= request.getAttribute("StaticHost") %>/js/jquery.dataTables.min.js"></script>--%>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrap-datepicker.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrap-datepicker.zh-CN.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/dataTables.bootstrap.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/adminlte.min.js"></script>
<script src="<%= request.getAttribute("StaticHost") %>/js/bootstrap-datetimepicker.min.js"></script>

<script>
    function gotoPage(){
        var p=$('#pz').val();
        if(p!=""){
            window.location.href="<%= url%>"+"&p="+p+"&pz=30";
        }
    }
    $(function () {
        $('#from, #to').datetimepicker({
            autoclose: true,
            todayBtn: true,
            clearBtn: true,
            language: "zh-CN"});
        /*$('#from, #to').datepicker({
            format: "yyyy-mm-dd hh:ii",
            todayBtn: true,
            clearBtn: true,
            language: "zh-CN"
        });*/

        $("[name='lnkSetSuccess']").click(function () {
            var orderId = $(this).attr("pk");
            if (confirm("确定置成功吗？")) {
                $.ajax({
                    url: "/order/" + orderId + "/setSuccess",
                    type: "PUT",
                    async: true,
                    context: this,
                    timeout: 10000,
                    success: function (data) {
                        if (null != data && data.code == 200) {
                            location.reload();
                        } else {
                            alert("失败:" + data);
                        }
                    },
                    error: function (d, s, b) {
                        alert("失败:" + b);
                    }
                });
            }
        });
        $("[name='lnkFreeze']").click(function () {
            var orderId = $(this).attr("pk");
            if (confirm("确定冻结吗？")) {
                $.ajax({
                    url: "/order/" + orderId + "/freeze",
                    type: "PUT",
                    async: true,
                    context: this,
                    timeout: 5000,
                    success: function (data) {
                        if (null != data && data.code == 200) {
                            location.reload();
                        } else {
                            alert("失败:" + data);
                        }
                    },
                    error: function (d, s, b) {
                        alert("失败:" + b);
                    }
                });
            }
        });
        $("[name='lnkUnfreeze']").click(function () {
            var orderId = $(this).attr("pk");
            if (confirm("确定解冻吗？")) {
                $.ajax({
                    url: "/order/" + orderId + "/unfreeze",
                    type: "PUT",
                    async: true,
                    context: this,
                    timeout: 5000,
                    success: function (data) {
                        if (null != data && data.code == 200) {
                            location.reload();
                        } else {
                            alert("失败:" + data);
                        }
                    },
                    error: function (d, s, b) {
                        alert("失败:" + b);
                    }
                });
            }
        });
        $("[name='lnkNotify']").click(function (event) {
            var tradeNo = $(this).attr("pk");
            $.ajax({
                url: "/order/" + tradeNo + "/notify",
                type: "PUT",
                async: true,
                context: this,
                timeout: 5000,
                success: function (data) {
                    if (null != data && data.code == 200) {
                        $('#'+tradeNo).find('button').html("已重发");
                        $(this).unbind('click');
                    } else {
                        alert("失败:" + data);
                    }
                },
                error: function (d, s, b) {
                    alert("失败:" + b);
                }
            });
        });
        initDownload();
    });

    $("#searchForm input, #searchForm select").change(initDownload);
    function initDownload() {
        $("#btnDownload").attr("href", "/order/export?" + $("#searchForm").serialize());
        //$("#btnDownload").attr("download", "订单明细 " + $("#from").val() + "至" + $("#to").val() + ".xls");
    }
</script>
</body>
</html>

